//
//  HNGetMegTableViewController.m
//  baidaifu
//
//  Created by 朱传正 on 2016/12/23.
//  Copyright © 2016年 好牛. All rights reserved.
//

#import "HNGetMegTableViewController.h"
#import "HNNormalMsgCell.h"
#import "HNDocterMsgCell.h"

@interface HNGetMegTableViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) NSMutableArray *dataSource;

@end

@implementation HNGetMegTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataSource = [NSMutableArray array];
    if (_typeIndex == 0) {
        self.title = @"系统消息";
    }else if (_typeIndex == 1) {
        self.title = @"订单消息";
    }
    
    [self.view setBackgroundColor:RGB(236, 240, 241)];
 
    self.tableView.bounces = NO;
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    [self initData];
}

-(void)initData{
    [self getMessageListData];
}

#pragma -mark TableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_dataSource count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
 
    return 110;
  
   
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *messageDic = [NSDictionary dictionaryWithDictionary:_dataSource[indexPath.row]];
    
    static NSString *CellIdentifier;
     CellIdentifier = @"HNNormalMsgCell";
    HNNormalMsgCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil]firstObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
   
    if (_typeIndex == 0) {
        
    }else if (_typeIndex == 1) {
        NSString *sendUserPhoto = [NSString stringWithFormat:@"%@",[messageDic objectForKey:@"sendUserPhoto"]];
         NSString *sendUserMobile = [NSString stringWithFormat:@"%@",[messageDic objectForKey:@"sendUserMobile"]];
        if ([AppConfig isNULL:sendUserPhoto]) {
              [cell.cellImage setImage:[UIImage imageNamed:@"icon-40.png"]];
        }else{
           [cell.cellImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",url_Base,sendUserPhoto]] placeholderImage:[UIImage imageNamed:@"Photo.png"]];
        }
     
      
       cell.cellTitle.text = [NSString stringWithFormat:@"%@",[messageDic objectForKey:@"title"]];
        
        if ([AppConfig isNULL:sendUserMobile]) {
             cell.cellRole.text = @"安徽益丰大药房";
        }else{
            cell.cellRole.text = sendUserMobile;
        }
      
         cell.cellTime.text = [NSString stringWithFormat:@"%@",[messageDic objectForKey:@"createTime"]];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",[messageDic objectForKey:@"content"]];
    }
    
    return cell;

  
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    HNGetMegTableViewController *getMegVc = [[HNGetMegTableViewController alloc]init];
//    
//    getMegVc.typeIndex = indexPath.row;
//    
//    NSLog(@"indexPath.row = %ld",indexPath.row);
//    
//    [self.navigationController pushViewController:getMegVc animated:YES];
    
    
}


#pragma mark - 获取消息
-(void)getMessageListData{
    
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:static_sessionId,@"sessionId",nil];
    
    NSLog(@"param = %@",param);
    
    NSString *urlStr = nil;
    if (_typeIndex == 0) {
        // 系统消息
        urlStr = url_message_list;
    }else if (_typeIndex == 1) {
        // 订单消息
        urlStr = url_message_list;
    }
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:urlStr parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"获取信息列表失败"];
            NSLog(@"==dict:%@",data);
            return;
        }
        NSLog(@"==dict:%@",data);
        
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
            NSDictionary * obj = [NSDictionary dictionaryWithDictionary:[data objectForKey:@"obj"]];
            NSLog(@"------obj = %@",obj);
            
            _dataSource = [NSMutableArray arrayWithArray:[obj objectForKey:@"result"]];
            
            NSLog(@"===self.docterListSource = %@",self.dataSource);
            
            [self.tableView reloadData];
            
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            if ([info isEqualToString:msg_InvalidLoginInformation]) {
                
                [AppConfig removeUserInfo];
                
                [[EMClient sharedClient] logout:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                
                [self.navigationController pushViewController:loginVC animated:YES];
            }
            [self showHint:info];
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull  error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"获取信息列表失败"];
        
    }];
    
    
}




@end
